package com.zhoug.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.zhoug.common.utils.LogUtils;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 自定义view:
 * -------------------------------
 * image  label     value image
 * -------------------------------
 * 全局样式：widgets_groupItemViewStyle
 *
 * @Author 35574
 * @Date 2020/7/17
 * @Description
 */
public class GroupItemView extends FrameLayout {
    private static final String TAG = ">>>GroupItemView";
    private static final boolean DEBUG = true;
    private ImageView mIvLeft;
    private TextView mTvLabel;
    private TextView mTvValue;
    private ImageView mIvRight;
    private boolean leftDrawableShow;
    private boolean rightDrawableShow;
    private static final ScaleType[] sScaleTypeArray = {
            ScaleType.MATRIX,
            ScaleType.FIT_XY,
            ScaleType.FIT_START,
            ScaleType.FIT_CENTER,
            ScaleType.FIT_END,
            ScaleType.CENTER,
            ScaleType.CENTER_CROP,
            ScaleType.CENTER_INSIDE
    };

    public GroupItemView(@NonNull Context context) {
        this(context, null);
    }

    public GroupItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.widgets_groupItemViewStyle);
    }

    public GroupItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);

    }

    private void initView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.widgets_group_item_view, this, false);
        mIvLeft = view.findViewById(R.id.iv_left);
        mTvLabel = view.findViewById(R.id.tv_label);
        mTvValue = view.findViewById(R.id.tv_value);
        mIvRight = view.findViewById(R.id.iv_right);
        addView(view);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GroupItemView, defStyleAttr, R.style.widgets_groupItemViewStyle);
        int leftResId = typedArray.getResourceId(R.styleable.GroupItemView_zmui_leftDrawable, 0);
        int leftDrawableMarginRight = typedArray.getDimensionPixelOffset(R.styleable.GroupItemView_zmui_leftDrawableMarginRight, 0);
        leftDrawableShow = typedArray.getBoolean(R.styleable.GroupItemView_zmui_leftDrawableShow, true);
        int rightResId = typedArray.getResourceId(R.styleable.GroupItemView_zmui_rightDrawable, 0);
        rightDrawableShow = typedArray.getBoolean(R.styleable.GroupItemView_zmui_rightDrawableShow, true);

        if (DEBUG) {
            LogUtils.d(TAG, "initView:leftDrawableShow=" + leftDrawableShow);
            LogUtils.d(TAG, "initView:rightDrawableShow=" + rightDrawableShow);
        }
        //label
        String label = typedArray.getString(R.styleable.GroupItemView_zmui_label);
        int labelSize = typedArray.getDimensionPixelOffset(R.styleable.GroupItemView_zmui_labelSize, 0);
        int labelColor = typedArray.getColor(R.styleable.GroupItemView_zmui_labelColor, 0);
        int labelMarginRight = typedArray.getDimensionPixelOffset(R.styleable.GroupItemView_zmui_labelMarginRight, 0);
        //value
        String value = typedArray.getString(R.styleable.GroupItemView_zmui_value);
        int valueSize = typedArray.getDimensionPixelOffset(R.styleable.GroupItemView_zmui_valueSize, 0);
        int valueColor = typedArray.getColor(R.styleable.GroupItemView_zmui_valueColor, 0);
        int valueMarginRight = typedArray.getDimensionPixelOffset(R.styleable.GroupItemView_zmui_valueMarginRight, 0);

        //把Style中的属性设置到ImageView中
        TypedValue typedValue = new TypedValue();
        if (typedArray.getValue(R.styleable.GroupItemView_zmui_leftDrawableStyle, typedValue)) {
            setImageStyle(context, mIvLeft, typedValue);
        }
        if (typedArray.getValue(R.styleable.GroupItemView_zmui_rightDrawableStyle, typedValue)) {
            setImageStyle(context, mIvRight, typedValue);
        }
        typedArray.recycle();

        //设置距离
        setMargin(mIvLeft, leftDrawableMarginRight);
        setMargin(mTvLabel, labelMarginRight);
        setMargin(mTvValue, valueMarginRight);

        setLeftDrawable(leftResId);
        setRightDrawable(rightResId);

        //label
        setLabel(label);
        setLabelSize(labelSize);
        setLabelColor(labelColor);
        //value
        setValue(value);
        setValueSize(valueSize);
        setValueColor(valueColor);
    }

    /**
     * 初始化imageStyle
     *
     * @param context
     * @param imageView
     * @param typedValue
     */
    private void setImageStyle(Context context, ImageView imageView, TypedValue typedValue) {
        if (typedValue.type == TypedValue.TYPE_REFERENCE) {
            int[] androidAttrs = new int[]{android.R.attr.layout_width,
                    android.R.attr.layout_height,
                    android.R.attr.tint,
                   R.attr.scaleType
            };
            TypedArray androidTypedArray = context.obtainStyledAttributes(typedValue.data, androidAttrs);
            int width = androidTypedArray.getLayoutDimension(0, ViewGroup.LayoutParams.WRAP_CONTENT);
            int height = androidTypedArray.getLayoutDimension(1, ViewGroup.LayoutParams.WRAP_CONTENT);
            ColorStateList colorStateList = androidTypedArray.getColorStateList(2);
            int scaleTypeIndex = androidTypedArray.getInt(3,-1);
            if (DEBUG) {
                LogUtils.d(TAG, "setImageStyle:width=" + width);
                LogUtils.d(TAG, "setImageStyle:height=" + height);
                LogUtils.d(TAG, "setImageStyle:scaleTypeIndex=" + scaleTypeIndex);
                LogUtils.d(TAG, "setImageStyle:colorStateList=" + colorStateList);
            }
            androidTypedArray.recycle();
            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            if (layoutParams != null) {
                layoutParams.width = width;
                layoutParams.height = height;
                imageView.setLayoutParams(layoutParams);
            }
            if (colorStateList != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    imageView.setImageTintList(colorStateList);
                }
            }
            if (scaleTypeIndex >= 0) {
                imageView.setScaleType(sScaleTypeArray[scaleTypeIndex]);
            }
        }
    }

    public void setLabel(CharSequence label) {
        mTvLabel.setText(label);
    }

    public void setLabelColor(int color) {
        mTvLabel.setTextColor(color);
    }

    public void setLabelSize(int textSize) {
        mTvLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    }

    public void setValue(CharSequence value) {
        mTvValue.setText(value);
    }

    public void setValueColor(int color) {
        mTvValue.setTextColor(color);
    }

    public void setValueSize(int textSize) {
        mTvValue.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    }

    public void setLeftDrawable(@DrawableRes int resId) {
        mIvLeft.setVisibility(leftDrawableShow ? VISIBLE : GONE);
        if (resId != 0) {
            mIvLeft.setImageResource(resId);
        }
    }

    public void setRightDrawable(@DrawableRes int resId) {
        mIvRight.setVisibility(rightDrawableShow ? VISIBLE : GONE);
        if (resId != 0) {
            mIvRight.setImageResource(resId);
        }
    }

    private void setMargin(View view, int rightMargin) {
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams != null) {
            if (layoutParams instanceof MarginLayoutParams) {
                ((MarginLayoutParams) layoutParams).rightMargin = rightMargin;
            }
            view.setLayoutParams(layoutParams);
        }
    }


}
